package Leetcode第二期;

import java.util.*;

/**
 * @author : K k
 * @date : 12:10 2020/10/2
 * 给定一个字符串数组，将字母异位词组合在一起。字母异位词指字母相同，但排列不同的字符串。
 *
 * 示例:
 *
 * 输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
 * 输出:
 * [
 *   ["ate","eat","tea"],
 *   ["nat","tan"],
 *   ["bat"]
 * ]
 */
public class 字母异位词分组_49 {

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res=new ArrayList<>();

        Map<String,List<String>> map=new HashMap<>();
        for (String s:strs){
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            if (map.containsKey(String.valueOf(chars))){
                map.get(String.valueOf(chars)).add(s);
            }else {
                ArrayList<String> ans = new ArrayList<>();
                ans.add(s);
                map.put(String.valueOf(chars),ans);
            }
        }
        for (List<String> ans:map.values()){
            res.add(ans);
        }
        return res;
    }

    public static void main(String[] args) {
        字母异位词分组_49 x=new 字母异位词分组_49();
        System.out.println(x.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }
}
